package com.mgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSONObject;
import com.player.DriverUser;
import com.player.WorldMgr;
import com.qy.DaoMgr;
import com.qy.bean.Option;
import com.qy.bean.OrderDriver;
import com.qy.bean.OrderInfo;
import com.qy.util.LogUtil;
import com.qy.util.StaticData;
import com.qy.util.SystemParamsUtil;
import com.qy.util.TimeUtil;


/**
 * 司机服务订单
 * @author zwl
 * 2018年2月3日
 */
public class DriverOrderMgr{

    //用户订单
    private static Map<Integer,OrderDriver> orderDriverMap = new ConcurrentHashMap<Integer,OrderDriver>(800);
    
    /**
     * 获取用户未完成的订单列表
     * @param driverId
     * @param status
     * @return
     */
    public static List<OrderDriver> getDriverOrder(int driverId,int status){
	List<OrderDriver> list = DaoMgr.getOrderDriverDao().getOrderDriverList(driverId, status);
	OrderDriver tmp = null;
	for(int i=0;i<list.size();i++){
	    tmp = list.get(i);
	    if(orderDriverMap.containsKey(tmp.getId())){
		list.set(i, orderDriverMap.get(tmp.getId()));
	    }else{
		orderDriverMap.put(tmp.getId(), tmp);
	    }
	}
	return list;
    }
    
    /**
     * 转换页面
     * @param od
     * @return
     */
    public static JSONObject turnToPage(int id){
	JSONObject js = new JSONObject();
	
	return js;
    }
    
    /**
     * 获取用户服务订单
     * @param driverId
     * @param status
     * @return
     */
    public static OrderDriver getDriverOrder(int id){
	OrderDriver orderDriver = orderDriverMap.get(id);
	if(orderDriver==null){
	    orderDriver =  DaoMgr.getOrderDriverDao().getOrderDriverById(id);
	    orderDriverMap.put(orderDriver.getId(), orderDriver);
	}
	return orderDriver;
    }
    /**
     * 创建一个服务订单
     * @param orderId
     * @param driverId
     */
    public static OrderDriver createDroverOrder(int orderId,int driverId){
	OrderDriver orderDriver = new OrderDriver();
	orderDriver.setOrderId(orderId);
	orderDriver.setStartTime(TimeUtil.getSysteCurTime());
	orderDriver.setDriverId(driverId);
	orderDriver.setStatus(StaticData.DriverOrderStatus.NORMAL.getType());
	orderDriver.setOp(Option.Insert);
	
	DaoMgr.getOrderDriverDao().createOrderDriver(orderDriver);
	orderDriverMap.put(orderDriver.getId(), orderDriver);
	return orderDriver;
    }
    
    /**
     * 司机开始服务
     * @param od
     * @param userId
     * @return
     */
    public static int startService(OrderDriver od,int userId){
	int code = StaticData.codeType.ok.getType();
	if (od == null) {
	    code = StaticData.codeType.not_exist.getType();
	    return code;
	}
	
	if (od.getDriverId() != userId) {
	    code=StaticData.codeType.NOT_THIS_DRIVER.getType();
	    return code;
	}
	
	od.setStatus(StaticData.DriverOrderStatus.START.getType());
	return code;
    }
    
    /**
     * 单笔结算
     * @param od
     * @throws Exception
     */
    public static int settle(OrderDriver od) throws Exception{
	int code = StaticData.codeType.ok.getType();
	if (od == null) {
	    code = StaticData.codeType.not_exist2.getType();
	    return code;
	}
	DriverUser dUser = (DriverUser)WorldMgr.getGamePlayer(od.getDriverId(),
		StaticData.userType.DRIVER_USER.getType());
	if(dUser==null){
	    code = StaticData.codeType.SETTLE_ERROR.getType();
	    return code;
	}
	od.setServiceCharge(SystemParamsUtil.SETTLE_DRIVER_MONEY());
	od.setStatus(StaticData.DriverOrderStatus.END.getType());
	dUser.setServerOrder(null);
	if(dUser.getUserType()!=StaticData.userType.CHECK_USER.getType()){
	    DriveRecordMgr.completOd(od.getId());
	    dUser.getDriverInfo().setMoney(dUser.getDriverInfo().getMoney()+SystemParamsUtil.SETTLE_DRIVER_MONEY());
	}
	return code;
    }
    
    /**
     * 创建服务好
     * @param userId
     * @param orderInfo
     */
    public static OrderDriver createOd(int userId,OrderInfo orderInfo){
	OrderDriver orderDriver = DriverOrderMgr.createDroverOrder(orderInfo.getId(), userId);
	//修改原来的订单状态为以接单
	if(orderInfo.getOrderStatus()==StaticData.OrderStatus.PAY_NOT_ACCEPT.getType()){
	    orderInfo.setOrderStatus(StaticData.OrderStatus.ACCEPT_NOT_ARRIVE.getType());
	    
		//设置取车服务号
	    orderInfo.setFDriver(orderDriver.getId());
	}else if(orderInfo.getOrderStatus()==StaticData.OrderStatus.COMPLET_CHECK_NOT_ACCEPT.getType()){
	    orderInfo.setOrderStatus(StaticData.OrderStatus.ACCEPT_NOT_ARRIVE.getType());
		//设置还车服务号
	    orderInfo.setTDriver(orderDriver.getId());
	}else{
	    //设置检车服务号
	    orderInfo.setSDriver(orderDriver.getId()); 
	}
	UserOrderMgr.update(orderInfo);
	
	return orderDriver;
    }
    
    public static void updateDB(OrderDriver orderDriver){
	DaoMgr.getOrderDriverDao().updateOrderDriver(orderDriver);
    }
    public static void save() {
	try {
	    List<OrderDriver> orderDriverList = new ArrayList<OrderDriver>(
		    orderDriverMap.values());
	    for (OrderDriver info : orderDriverList) {
		updateDB(info);
		if (TimeUtil.getSysCurTimeMillis() / 1000
			- info.getLastUseTime() > 60 * 60) {
		    if (info.getOp() == Option.None) {
			info.setOp(Option.Close);
		    } else if (info.getOp() == Option.Close) { // 清除离线玩家数据
			// 离线超过五分钟
			orderDriverMap.remove(info.getId());
			LogUtil.info("清除玩家orderDriverMap缓存数据:" + info.getId());
		    }
		}
	    }
	} catch (Exception e) {
	    LogUtil.error("保存OrderInfo数据报错");
	}

    }

}
